
$(function () {
    var collect = {
        list: $('.list'),
        myScroll: '',
        init: function () {
            // 禁止微信webview网页下拉
            document.querySelector('body').addEventListener('touchmove', function(e) {
                e.preventDefault();
            })
            this.event();
        },
        event: function () {
            // 获取收藏列表
            this.getCollects();
            // 上拉下拉
            this.scrollData();
        },
        // 获取收藏列表
        getCollects: function () {
            var self = this , ajax = new AjaxHelper();
            ajax.get('/user/collects/list', '', function (data) {
                // 判断是否有值
                if (data.results.length <= 0) {
                    self.list.append('<p class="noData">没有数据， 快去收藏吧 ~ </p>'); 
                } else {
                    $.each(data.results, function (i, item) {
                    var t = simpleTpl();
                        if (item.post.elite == true) {
                            var jh = '<i class="iconfont card-type card-type-jh">&#xe633;</i>';
                        } else {
                            var jh = '';
                        }
                        t._('<div class="list-item img-txt-list-item" data-path="/article/detail/'+item.post._id+'">')
                            ._('<a href="/article/'+item.post._id+'">')
                                ._('<div class="pull-right txt-item">')
                                    ._('<p class="item-title">'+item.post.title+'<span class="type type-'+item.post.category.slug+'">'+item.post.category.name+'</span></p>')
                                    ._('<p class="item-desc">'+item.post.body+'</p>')
                                    ._('<div class="item-other">')
                                        ._('<span class="author"><i class="iconfont">&#xe910;</i><b>'+item.post.author.username+'</b></span>')
                                        ._('<span class="address"><i class="iconfont">&#xe939;</i>'+item.post.area+'</span>')
                                        ._('<span class="date">'+moment(item.post.created_at,'YYYYMMDD').fromNow()+'</span>')
                                        ._('<span class="comments"><i class="iconfont">&#xe957;</i><b>'+item.post.comments.length+'</b></span>')
                                        ._('<span class="skim"><i class="iconfont">&#xe8db;</i><b>'+item.post.skim_num+'</b></span>')
                                    ._('</div>')
                                ._('</div>')
                                ._(jh)
                            ._('</a>')
                        t._('</div');

                        self.list.attr({
                            'data-pageNumber': data.pageNumber,
                            'data-pageCount': data.pageCount
                        });
                        self.list.append(t.toString());
                        self.myScroll.refresh();
                        $.hideLoading();
                    });
                }
            });
        },
        scrollData: function () {
            var self = this;
                upIcon = $("#up-icon"),
                downIcon = $("#down-icon");
                
            self.myScroll = new IScroll('#fotxt-wrapper', { probeType: 3, mouseWheel: true, preventDefault: false});
            
            self.myScroll.on("scroll",function(){
                var y = this.y,
                    maxY = this.maxScrollY - y,
                    downHasClass = downIcon.hasClass("reverse_icon"),
                    upHasClass = upIcon.hasClass("reverse_icon");
                
                if(y >= 40){
                    !downHasClass && downIcon.addClass("reverse_icon");
                    return "";
                }else if(y < 40 && y > 0){
                    downHasClass && downIcon.removeClass("reverse_icon");
                    return "";
                }
                
                if(maxY >= 40){
                    !upHasClass && upIcon.addClass("reverse_icon");
                    return "";
                }else if(maxY < 40 && maxY >=0){
                    upHasClass && upIcon.removeClass("reverse_icon");
                    return "";
                }
            });
            // 下拉
            self.myScroll.on("slideDown",function(){
                if(this.y > 40){
                    self.slideDownFn();
                }
            });
            
            self.myScroll.on("slideUp",function(){
                if(this.maxScrollY - this.y > 40){
                    // 如果导航选中项和轮播选中项一样则进行下一步
                    var pageNumber = self.list.attr('data-pagenumber');
                    var pageCount = self.list.attr('data-pagecount');
                    // 如果当前页为最后一页 则直接跳出
                    if (pageNumber == pageCount) {
                        $.toptips('没有数据了！');
                        self.myScroll.refresh();
                        return false;
                    }
                    var page = parseInt(pageNumber);
                    var ajax = new AjaxHelper();
                    ajax.get('/user/collects/list?page='+(++page)+'', '', function (data) {
                        $.hideLoading();
                        var results = data.results;
                        if (results.length > 0) {
                            $.each(data.results, function (i, item) {
                                var t = simpleTpl();
                                if (item.post.elite == true) {
                                    var jh = '<i class="iconfont card-type card-type-jh">&#xe633;</i>';
                                } else {
                                    var jh = '';
                                }
                                t._('<div class="list-item img-txt-list-item" data-path="/article/detail/'+item.post._id+'">')
                                    ._('<a href="/article/'+item.post._id+'">')
                                        ._('<div class="pull-right txt-item">')
                                            ._('<p class="item-title">'+item.post.title+'<span class="type type-'+item.post.category.slug+'">'+item.post.category.name+'</span></p>')
                                            ._('<p class="item-desc">'+item.post.body+'</p>')
                                            ._('<div class="item-other">')
                                                ._('<span class="author"><i class="iconfont">&#xe910;</i><b>'+item.post.author.username+'</b></span>')
                                                ._('<span class="address"><i class="iconfont">&#xe939;</i>'+item.post.area+'</span>')
                                                ._('<span class="date">'+moment(item.post.created_at,'YYYYMMDD').fromNow()+'</span>')
                                                ._('<span class="comments"><i class="iconfont">&#xe957;</i><b>'+item.post.comments.length+'</b></span>')
                                                ._('<span class="skim"><i class="iconfont">&#xe8db;</i><b>'+item.post.skim_num+'</b></span>')
                                            ._('</div>')
                                        ._('</div>')
                                        ._(jh)
                                    ._('</a>')
                                t._('</div');

                                self.list.attr({
                                    'data-pageNumber': data.pageNumber,
                                    'data-pageCount': data.pageCount
                                });
                                // 为了测试，延迟1秒加载
                                setTimeout(function(){
                                    self.list.append(t.toString());
                                    $.hideLoading();
                                    // 每次数据加载完，必须重置
                                    self.myScroll.refresh()
                                },1000);
                            });
                        } else {
                            self.myScroll.refresh();
                            $.hideLoading();
                        }
                    }, function() {
                        $.toptips('数据获取失败！');
                        // 即使加载出错，也得重置
                        self.myScroll.refresh();
                        $.hideLoading();
                    });
                    upIcon.removeClass("reverse_icon")
                }
            });
        },
        slideDownFn: function () {
            var self = this;
                upIcon = $("#up-icon"),
                downIcon = $("#down-icon");

                // 获取当前选中文章类型
                var ajax = new AjaxHelper();
                // 获取当前类型文章集合
                ajax.get('/user/collects/list', '', function (data) {
                    // 如果结果长度大于0
                    if (data.results.length > 0) {
                        // 有结果就清空列表
                        self.list.empty();
                        $.each(data.results, function (i, item) {
                            var t = simpleTpl();
                            if (item.post.elite == true) {
                                var jh = '<i class="iconfont card-type card-type-jh">&#xe633;</i>';
                            } else {
                                var jh = '';
                            }
                            t._('<div class="list-item img-txt-list-item" data-path="/article/detail/'+item.post._id+'">')
                                ._('<a href="/article/'+item.post._id+'">')
                                    ._('<div class="pull-right txt-item">')
                                        ._('<p class="item-title">'+item.post.title+'<span class="type type-'+item.post.category.slug+'">'+item.post.category.name+'</span></p>')
                                        ._('<p class="item-desc">'+item.post.body+'</p>')
                                        ._('<div class="item-other">')
                                            ._('<span class="author"><i class="iconfont">&#xe910;</i><b>'+item.post.author.username+'</b></span>')
                                            ._('<span class="address"><i class="iconfont">&#xe939;</i>'+item.post.area+'</span>')
                                            ._('<span class="date">'+moment(item.post.created_at,'YYYYMMDD').fromNow()+'</span>')
                                            ._('<span class="comments"><i class="iconfont">&#xe957;</i><b>'+item.post.comments.length+'</b></span>')
                                            ._('<span class="skim"><i class="iconfont">&#xe8db;</i><b>'+item.post.skim_num+'</b></span>')
                                        ._('</div>')
                                    ._('</div>')
                                    ._(jh)
                                ._('</a>')
                            t._('</div');

                            self.list.attr({
                                'data-pageNumber': data.pageNumber,
                                'data-pageCount': data.pageCount
                            });
                            // 为了测试，延迟1秒加载
                            setTimeout(function(){
                                self.list.append(t.toString());
                                $.hideLoading();
                                // 每次数据加载完，必须重置
                                self.myScroll.refresh()
                            },1000);
                        });
                    } else {
                        $.hideLoading();
                        // 每次数据加载完，必须重置
                        self.myScroll.refresh()
                        $.toptips('没有数据了 ～');
                    }
                }, function() {
                    $.toptips('数据获取失败！');
                    // 即使加载出错，也得重置
                    self.myScroll.refresh();
                    $.hideLoading();
                });
                upIcon.removeClass("reverse_icon");
        }
    }
    collect.init();    
})